
As we all know how this virus is spreading very dangerously in the communities and localities. While kaggle community is working hard to show how is the spreading trend is in the different countries of the world .The trends of COVID in india is not lesser known as it is spreading very rapidly in the indian subcontinent but measures taken by Indian government are good and essential but still we should prepare for a long fight against this pandemic until scientists all around the world could find a breakthrough.Until than STAY INDOORS and follow the guidelinces of the authorities. STAY SAFE
Please UPVOTE this kernel if you like it. It motivates me to produce more quality content :)
Please do comment what your views are and what you understand from this.
Dont't forget to give your suggestions in the comment section
This notebook is my contribution to show how the medical situations are in India and what are estimates and trends.
Credit - Whole Kaggle community but special thanks to
It might take some time to load
!pip install chart_studio
!pip install pgeocode
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
pio.templates.default = "plotly_dark"
from plotly.subplots import make_subplots
from pathlib import Path
data_dir = Path('../input/covid19-corona-virus-india-dataset')
import os
os.listdir(data_dir)
df = pd.read_csv('../input/covid19-corona-virus-india-dataset/patients_data.csv')
from IPython.display import display, HTML
display(HTML(df.tail().to_html()))
Shows the information about the number of patients this dataset is updated in every 24 hours till today we see their are 11512 cases of covid in india
a little info - i started making this notebook yesterday and there where about near 9500 patients so you can see how sevierly the number is increasing
def state_wise_patients(name,df=df):
data = df.loc[df['detected_state']==name]
df = data[['patient_number','date_announced','detected_state']]
data = df.groupby('date_announced')['patient_number'].nunique()
data = data.reset_index()
data['date_announced']=pd.to_datetime(data['date_announced'],format = '%d/%m/%Y')
data = data.sort_values(by=['date_announced'], ascending=True)
data['patient_number'] = data.patient_number.cumsum()
return data
This function segregates and collects the data of different states with dates and patient_number
collection = {}
for i in df.detected_state.unique():
collection['patients in '+ str(i)] = state_wise_patients(i)
for instance see collection['patients in Maharashtra']
collection['patients in Maharashtra']
keys = list(collection.keys())
visible_True=[]
for i in range(len(keys)):
visible_True.append(True)
def t2f(i):
visible = []
for a in range(len(keys)):
if a == i:
visible.append(True)
else:
visible.append(False)
return visible
def create_buttons(keys=keys):
l=[dict(label = 'All',
method = 'update',
args = [{'visible': visible_True},
{'title': 'All',
'showlegend':True}])]
for i in range(len(keys)):
l.append(dict(label = keys[i],
method = 'update',
args = [{'visible': t2f(i)}, # the index of True aligns with the indices of plot traces
{'title': keys[i],
'showlegend':True}]))
return l
fig = go.Figure()
keys = list(collection.keys())
for column in collection:
fig.add_trace(
go.Line(
x = collection[column].date_announced,
y = collection[column].patient_number,
name = column
)
)
#fig.update_layout(updatemenus=[go.layout.Updatemenu( active=0,buttons=list(create_buttons()))])
fig.show()
we will analyze these states
you can see the plots of these states either by clicking the legend or by the dropdown list
def per_day_inc(collection=collection):
for i in list(collection.keys()):
collection[i]['prev_patients'] = collection[i]['patient_number'].shift(1)
collection[i]['new_patients'] = collection[i]['patient_number'] - collection[i]['prev_patients']
return collection
coll1 = per_day_inc()
fig = go.Figure()
keys = list(collection.keys())
for column in collection:
fig.add_trace(
go.Line(
x = collection[column].date_announced,
y = collection[column].new_patients,
name = column
)
)
fig.update_layout(updatemenus=[go.layout.Updatemenu(active=0,buttons=list(create_buttons()))])
fig.show()
you can see the plots of these states either by clicking the legend or by the dropdown list
fig = go.Figure()
keys = list(collection.keys())
for column in collection:
fig.add_trace(
go.Line(
x = collection[column].date_announced,
y = collection[column].patient_number,
name = column
)
)
fig.update_layout(yaxis_type='log')
fig.show()
import requests
india_data_json = requests.get('https://api.rootnet.in/covid19-in/unofficial/covid19india.org/statewise').json()
df_india = pd.io.json.json_normalize(india_data_json['data']['statewise'])
df_india = df_india.set_index("state")
total = df_india.sum()
total.name = "Total"
df_t = pd.DataFrame(total,dtype=float).transpose()
df_t["Mortality Rate (per 100)"] = np.round(100*df_t["deaths"]/df_t["confirmed"],2)
df_india["Mortality Rate (per 100)"]= np.round(np.nan_to_num(100*df_india["deaths"]/df_india["confirmed"]),2)
df_india.style.background_gradient(cmap='Blues',subset=["confirmed"])\
.background_gradient(cmap='Reds',subset=["deaths"])\
.background_gradient(cmap='Greens',subset=["recovered"])\
.background_gradient(cmap='Purples',subset=["active"])\
.background_gradient(cmap='plasma',subset=["Mortality Rate (per 100)"])
the above table is self explainatory it shows the most essential stats about the COVID situation (confirmed recovered deaths active Mortality Rate (per 100)) with darker color means high values
trace1 = go.Bar(
x = df_india.index,
y = df_india.deaths,
name = "deaths",
marker = dict(color = 'rgba(255, 174, 255, 0.5)',
line=dict(color='rgb(0,0,0)',width=1.5)),
text = df_india.index)
# create trace2
trace2 = go.Bar(
x = df_india.index,
y = df_india.recovered,
name = "recovered",
marker = dict(color = 'rgba(255, 255, 128, 0.5)',
line=dict(color='rgb(0,0,0)',width=1.5)),
text = df_india.index)
trace3 = go.Bar(
x = df_india.index,
y = df_india.active,
name = "active",
marker = dict(color = 'rgba(0, 174, 174, 0.5)',
line=dict(color='rgb(0,0,0)',width=1.5)),
text = df_india.index)
data = [trace1, trace2,trace3]
layout = go.Layout(barmode = "group")
fig = go.Figure(data = data, layout = layout)
fig.update_layout(yaxis_type="log")
fig.show()
Simple bar chart in (log scale)showing the huge difference in the active and recovered cases
import plotly.express as px
import numpy as np
fig = px.scatter_3d(df_india, x='recovered', y='active', z='confirmed',size='confirmed', color=df_india.index)
fig.update_layout(height=800, width=800,scene_zaxis_type="log",scene_yaxis_type="log",scene_xaxis_type="log")
fig.show()
3D scatter plot showing a great pattern all of the data points are aligned in a linear fashion reason ??(tell in comments)
import plotly.graph_objects as go
from plotly.subplots import make_subplots
labels = df_india.index
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=labels, values=df_india.deaths, name="deaths"),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=df_india["Mortality Rate (per 100)"], name="Mortality Rate (per 100)"),
1, 2)
# Use `hole` to create a donut-like pie chart
fig.update_traces(hole=.4, hoverinfo="label+percent+name")
fig.update_layout(
title_text="Covid-19 ",
# Add annotations in the center of the donut pies.
annotations=[dict(text='deaths', x=0.18, y=0.5, font_size=20, showarrow=False),
dict(text='Mortality', x=0.82, y=0.5, font_size=20, showarrow=False)])
fig.show()
data_dir = Path('/kaggle/input/hospitalbedloc')
import folium
india = folium.Map(location=[20.5937,78.9629], zoom_start=5.4)
df = pd.read_csv('../input/covid19-corona-virus-india-dataset/complete.csv')
for lat, lon,State,Death,Total_confirmed_cases in zip(df['Latitude'], df['Longitude'],df['Name of State / UT'],df['Death'],df['Total Confirmed cases']):
folium.CircleMarker([lat, lon],
radius=5,
color='Blue',
popup =('State:' + str(State) + '<br>'
'Total Confirmed cases:' + str(Total_confirmed_cases) + '<br>',
'Deaths :' + str(Death) +'<br>'
),
fill_color='red',
fill_opacity=0.7 ).add_to(india)
india
geo spacial veiw of patients in different states of india (hover to view)
Now we will analyze the Hospitals and beds available in different states

df = pd.read_csv(data_dir/'HospitalBedsIndiaLocations.csv')
df = df.fillna(0)
df['total_Beds'] = df['NumUrbanBeds_NHP18'] + df['NumRuralBeds_NHP18'] + df['NumPublicBeds_HMIS']
df['total_Hospitals'] = df['NumUrbanHospitals_NHP18'] + df['NumRuralHospitals_NHP18'] + df['NumSubDistrictHospitals_HMIS'] + df['NumDistrictHospitals_HMIS']
#india = folium.Map(location=[20.5937,78.9629], zoom_start=5.4,max_zoom=10)
for i in range(len(df['State/UT'].values)):
folium.Circle(
location=[df.loc[i]['Latitude'], df.iloc[i]['Longitude']],
tooltip = "<h5 style='text-align:center;font-weight: bold'>"+ str(df.iloc[i]['State/UT'])+"</h5>"+
"<div style='text-align:center;'>"+"total_Beds: " + str(df.iloc[i]['total_Beds'])+"</div>"+
"<div style='text-align:center;'>"+"total_Hospital: " + str(df.iloc[i]['total_Hospitals'])+"</div>"+
"<hr style='margin:10px;'>"+
"<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>"+
"<li>NumSubDistrictHospitals_HMIS: "+str(df.iloc[i]['NumSubDistrictHospitals_HMIS'])+"</li>"+
"<li>NumDistrictHospitals_HMIS: "+str(df.iloc[i]['NumDistrictHospitals_HMIS'])+"</li>"+
"<li>TotalPublicHealthFacilities_HMIS: "+str(df.iloc[i]['TotalPublicHealthFacilities_HMIS'])+"</li>"+
"<li>NumPublicBeds_HMIS: "+str(df.iloc[i]['NumPublicBeds_HMIS'])+"</li>"+
"<li>NumRuralHospitals_NHP18: "+str(df.iloc[i]['NumRuralHospitals_NHP18'])+"</li>"+
"<li>NumRuralBeds_NHP18: "+str(df.iloc[i]['NumRuralBeds_NHP18'])+"</li>"+
"<li>NumUrbanHospitals_NHP18: " + str(df.iloc[i]['NumUrbanHospitals_NHP18'])+"</li>"+
"<li>NumUrbanBeds_NHP18: " + str(df.iloc[i]['NumUrbanBeds_NHP18'])+"</li>"+
"<li>NumCommunityHealthCenters_HMIS: " + str(df.iloc[i]['NumCommunityHealthCenters_HMIS'])+"</li>"+
"</ul>",
radius=int((np.log2(df.iloc[i]['total_Beds']+1))*6000),
color=df['State/UT'].values[i],
fill_color='red',
fill=True).add_to(india)
india
Geo spacial view about the number of medical care facilities available in indian states (hover to view)
data_dir1 = Path('/kaggle/input/covid19-in-india')
df1 = pd.read_csv(data_dir1/'AgeGroupDetails.csv')
import plotly.graph_objects as go
from plotly.subplots import make_subplots
labels = df1.AgeGroup
values = df1.TotalCases
# Create subplots: use 'domain' type for Pie subplot
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'xy'}, {'type':'domain'}]])
fig.add_trace(go.Bar(x=labels, y=values, name="bar",marker = dict(color = 'rgba(0, 174, 174, 0.5)',
line=dict(color='rgb(0,0,0)',width=1.5)),
text = labels),
1, 1)
fig.add_trace(go.Pie(labels=labels, values=values, name="pie"),
1, 2)
fig.update_layout(
title_text="Covid-19 Age group details ")
# Add annotations in the center of the donut pies.
fig.show()
Using pgeocode library to get latitude and longitude from the pincode
Here i have tried to show the distribtution of ICMR labs around the states

import pgeocode
df1 = pd.read_csv('../input/covid19-in-india/ICMRTestingLabs.csv')
nomi = pgeocode.Nominatim('IN')
lat = []
long = []
for i in df1.pincode.values:
lat.append(nomi.query_postal_code(str(i)).latitude)
long.append(nomi.query_postal_code(str(i)).longitude)
df1['lat'] = pd.Series(lat)
df1['long'] = pd.Series(long)
df1 = df1.dropna()
df1 = df1.reset_index()
import folium
#india = folium.Map(location=[20.5937,78.9629], zoom_start=5.4,max_zoom=10)
for i in range(len(df1)):
folium.Circle(
location=[df1.loc[i]['lat'], df1.iloc[i]['long']],
tooltip = "<h5 style='text-align:center;font-weight: bold'>"+ str(df1.iloc[i]['state'])+"</h5>"+
"<div style='text-align:center;'>"+"type: " + str(df1.iloc[i]['type'])+"</div>"+
"<div style='text-align:center;'>"+"city: " + str(df1.iloc[i]['city'])+"</div>"+
"<hr style='margin:10px;'>"+
"<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>"+
"<li>lab : " + str(df1.iloc[i]['lab'])+"</li>"+
"<li>address : " + str(df1.iloc[i]['address'])+"</li>"+
"</ul>",
radius=30000,
color=df1['state'].values[i],
fill_color='red',
fill=True).add_to(india)
india
This Map plot shows where are the labs are situated in different regions of india and which state has more labs(Hover to see)
Now i have described how the trends of testing and +ve cases connects with each other
df1 = pd.read_csv('../input/covid19-in-india/ICMRTestingDetails.csv')
import plotly.graph_objects as go
x1 = df1.DateTime.values
x=df1.TotalSamplesTested.values
y = df1.TotalIndividualsTested.values
z = df1.TotalPositiveCases
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x1, y=z,
hoverinfo='x+y',
mode='lines',
line=dict(width=0.5, color='rgb(256, 0, 256)'),
stackgroup='one',
name = 'TotalPositiveCases'
))
fig.add_trace(go.Scatter(
x=x1, y=x,
hoverinfo='x+y',
mode='lines',
line=dict(width=0.5, color='rgb(0, 256, 256)'),
stackgroup='one',
name ='TotalSamplesTested'
))
fig.update_layout()
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x1, y=z,
hoverinfo='x+y',
mode='lines',
line=dict(width=0.5, color='rgb(256, 0, 256)'),
stackgroup='one',
name = 'TotalPositiveCases'
))
fig.add_trace(go.Scatter(
x=x1, y=x,
hoverinfo='x+y',
mode='lines',
line=dict(width=0.5, color='rgb(0, 256, 256)'),
stackgroup='one',
name ='TotalSamplesTested'
))
fig.update_layout(yaxis_type="log")
fig.show()
df = pd.read_csv(data_dir/'HospitalBedsIndiaLocations.csv')
df =df.fillna(0)
df['total_Beds'] = df['NumUrbanBeds_NHP18'] + df['NumRuralBeds_NHP18'] + df['NumPublicBeds_HMIS']
df['total_Hospitals'] = df['NumUrbanHospitals_NHP18'] + df['NumRuralHospitals_NHP18'] + df['NumSubDistrictHospitals_HMIS'] + df['NumDistrictHospitals_HMIS']
df.index=df['State/UT']
df = df.drop(columns=['Sno','State/UT'])
Aggregating the Beds and Hospitals in different states
df1 = pd.read_csv('../input/covid19-in-india/population_india_census2011.csv')
df1 = df1.sort_values(by='State / Union Territory')
df1 = df1.reset_index()
df1.index = df1['State / Union Territory']
df1 = df1.drop(columns=['index','Sno','State / Union Territory'])
from IPython.display import display, HTML
display(HTML(df1.to_html()))
df.index.values[0] = df1.index.values[0]
df.index.values[-5] = df1.index.values[-5]
df.index.values[14] = df1.index.values[13]
df['Population'] = df1['Population']
df['Rural population'] = df1['Rural population']
df['Urban population'] = df1['Urban population']
df = df.drop(['Dadra & Nagar Haveli','Daman & Diu'],axis=0)
df['total_Rural_Hospitals'] = df['NumRuralHospitals_NHP18'] + df['NumSubDistrictHospitals_HMIS'] + df['NumDistrictHospitals_HMIS']
df['total_Rural_Beds'] = df['NumRuralBeds_NHP18'] + df['NumPublicBeds_HMIS']
df['total_Urban_Hospitals'] = df['NumUrbanHospitals_NHP18'] + df['NumSubDistrictHospitals_HMIS'] + df['NumDistrictHospitals_HMIS']
df['total_Urban_Beds'] = df['NumUrbanBeds_NHP18'] + df['NumPublicBeds_HMIS']
df['total_medical_centres'] = df['NumPrimaryHealthCenters_HMIS'] + df['NumCommunityHealthCenters_HMIS'] + df['TotalPublicHealthFacilities_HMIS']
New columns
df["Hospitals (per 100000)"]= np.round(100000*df["total_Hospitals"]/df["Population"],2)
df["Beds (per 100000)"]= np.round(100000*df["total_Beds"]/df["Population"],2)
df["rural Hospitals (per 100000)"]= np.round(100000*df["total_Rural_Hospitals"]/df["Rural population"],2)
df["rural Beds (per 100000)"]= np.round(100000*df["total_Rural_Beds"]/df["Rural population"],2)
df["Urban Hospitals (per 100000)"]= np.round(100000*df["total_Urban_Hospitals"]/df["Urban population"],2)
df["Urban Beds (per 100000)"]= np.round(100000*df["total_Urban_Beds"]/df["Urban population"],2)
df = df[['total_Rural_Beds','total_Urban_Hospitals','total_Urban_Beds','total_medical_centres','Hospitals (per 100000)','Beds (per 100000)'
,'rural Hospitals (per 100000)','rural Beds (per 100000)','Urban Hospitals (per 100000)','Urban Beds (per 100000)']]
df_india = df_india.sort_index()
df['confirmed'] = df_india['confirmed']
df['recovered'] = df_india['recovered']
df['deaths'] = df_india['deaths']
df['active'] = df_india['active']
df['Mortality Rate (per 100)'] = df_india['Mortality Rate (per 100)']
df=df.dropna()
df.style.background_gradient(cmap='Blues',subset=["Beds (per 100000)"])\
.background_gradient(cmap='Reds',subset=["Urban Hospitals (per 100000)"])\
.background_gradient(cmap='Greens',subset=["rural Hospitals (per 100000)"])\
.background_gradient(cmap='Purples',subset=["rural Beds (per 100000)"])\
.background_gradient(cmap='YlOrBr',subset=["Urban Beds (per 100000)"])\
.background_gradient(cmap='Oranges',subset=["Hospitals (per 100000)"])\
.background_gradient(cmap='Reds',subset=["Mortality Rate (per 100)"])\
.background_gradient(cmap='Purples',subset=["confirmed"])\
.background_gradient(cmap='Greens',subset=["deaths"])\
.background_gradient(cmap='Blues',subset=["active"])\
.background_gradient(cmap='Oranges',subset=["recovered"])\
import plotly.graph_objects as go
fig = go.Figure(data=go.Heatmap(
z=df.corr(),
x=df.columns.values,
y=df.columns.values,
hoverongaps = False))
fig.show()
df = pd.read_csv('../input/covid19-corona-virus-india-dataset/patients_data.csv')
a = df.detected_district.value_counts()
a = a.dropna()
But why does Mumbai is worst hit
Maharashtra is bearing the brunt of India’s COVID-19 crisis, with 23% of the total cases and 46% of overall deaths*. Most cases are from Mumbai, with the highest share in its G-south ward. Wards with dense populations have the highest number of cases. The State has the highest number of such wards. for more you can see

!pip install opencage
from opencage.geocoder import OpenCageGeocode
key = '157336e14b654bceb51b5fc3cc07bec4' # get api key from: https://opencagedata.com
geocoder = OpenCageGeocode(key)
query = 'Bid, India'
results = geocoder.geocode(query)
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
print (lat, lng)
from scipy.optimize import curve_fit
import matplotlib.pyplot as pt
def log_curve(x, k, x_0, ymax):
return ymax / (1 + np.exp(-k*(x-x_0)))
def fit_curve():
for i in list(collection.keys()):
if collection[i]['patient_number'].max()>650:
x_data = range(len(collection[i].index))
y_data = collection[i]['patient_number']
popt, pcov = curve_fit(log_curve, x_data, y_data, bounds=([0,0,0],np.inf), maxfev=50000)
estimated_k, estimated_x_0, ymax= popt
k = estimated_k
x_0 = estimated_x_0
y_fitted = log_curve(range(0,160), k, x_0, ymax)
fig = go.Figure()
fig.add_trace(go.Line(x = np.arange(160),y = y_fitted,name ='predicted ' + i ))
fig.add_trace(go.Line(x = np.arange(80),y = y_data,name = i))
fig.show()
fit_curve()

Well in all of this negativity we can see some remarkable changes in our environment and air quality
but due to the severe fatalities covid has inflicted on makes me sad irrespective of what is the air quality during the lockdown period.
but i also think that the government has spent a lot of money on to reduce the air,water pollution but we never saw this level of changes
so in my though government should learn from this lockdown and try to regulate their environment saving policies